package spring.cloud.tasks.executor.name_space.executor.task.task_listener.task_configuration;

import com.google.common.base.Strings;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import spring.cloud.tasks.common.node.TaskNodePath;
import spring.cloud.tasks.executor.name_space.NameSpaceDataManager;


@Slf4j
@Setter
@Getter
public class TaskConfiguration {
    private final String taskId;
    /**
     * 如果regCenter!=null, 取值从zk取
     */
    private NameSpaceDataManager nameSpaceDataManager;
    private String taskType;
    private String linkId;//link id针对同一个task的bean id需要唯一
    private boolean enabled = true;
    private String description = "";
    //
    /**
     * 任务启动时间的cron表达式.
     */
    private String cronExpressionUpdateId;
    private String cronExpression;
    /**
     * 任务暂停时间段，日期段。
     */
    private String pauseDatePeriod = "";
    /**
     * 任务暂停时间段，小时分钟段。
     */
    private String pauseTimePeriod = "";
    /**
     * 提前进行预分配时间:单位分钟
     */
    private Integer preAllotMinutes;
    //
    /**
     * 任务分片总数.
     */
    private int taskItemTotalCount;
    /**
     * 可能被propertiesString里面的属性值替换
     */
    private String taskItemIndexAndTaskItemParametersString = "";
    private String propertiesString = "";
    //
    /**
     * 每个任务的预分配列表
     */
    private String preferExecutorIdList = "";
    /**
     * 是否使用非preferList
     */
    private boolean useDisExecutorIdList = true;
    //
    /**
     * 每个分片的权重
     */
    private Integer loadLevel = 1;


    /**
     * 任务允许延迟执行的秒数
     */
    private Integer secondAfterTaskStopCommandInstructed;
    /**
     * Job超时时间
     */
    private int timeoutMinutes;

    public TaskConfiguration(NameSpaceDataManager nameSpaceDataManager, String taskId) {
        this.taskId = taskId;
        this.nameSpaceDataManager = nameSpaceDataManager;
        reloadTaskConfiguration();
    }

    /**
     * update cron cache and return it
     */

    public String getCronExpressionUpdateIdFromZooKeeper() {
        cronExpressionUpdateId = nameSpaceDataManager.getData(TaskNodePath.getTaskNodePath(taskId, TaskConfigurationNode.cron_expression_update_id));
        return cronExpression;
    }

    public String getCronExpressionFromZooKeeper() {
        cronExpression = nameSpaceDataManager.getData(TaskNodePath.getTaskNodePath(taskId, TaskConfigurationNode.cron));
        return cronExpression;
    }

    public String getPauseDatePeriodFromZooKeeper() {
        pauseDatePeriod = nameSpaceDataManager.getData(TaskNodePath.getTaskNodePath(taskId, TaskConfigurationNode.pause_date_period));
        return pauseDatePeriod;
    }

    public String getPauseTimePeriodFromZooKeeper() {
        pauseTimePeriod = nameSpaceDataManager.getData(TaskNodePath.getTaskNodePath(taskId, TaskConfigurationNode.pause_time_period));
        return pauseTimePeriod;
    }

    public boolean isDeleting() {
        return nameSpaceDataManager.checkExists(TaskNodePath.getTaskNodePath(taskId, TaskConfigurationNode.to_delete));
    }

    public void reloadTaskConfiguration() {
        if (nameSpaceDataManager == null) {
            return;
        }
        //
        taskType = nameSpaceDataManager.getData(TaskNodePath.getTaskNodePath(taskId, TaskConfigurationNode.task_type));
        linkId = nameSpaceDataManager.getData(TaskNodePath.getTaskNodePath(taskId, TaskConfigurationNode.task_class));
        enabled = Boolean.parseBoolean(nameSpaceDataManager.getData(TaskNodePath.getTaskNodePath(taskId, TaskConfigurationNode.enabled)));
        description = nameSpaceDataManager.getData(TaskNodePath.getTaskNodePath(taskId, TaskConfigurationNode.description));
        //
        cronExpression = nameSpaceDataManager.getData(TaskNodePath.getTaskNodePath(taskId, TaskConfigurationNode.cron));
        pauseDatePeriod = nameSpaceDataManager.getData(TaskNodePath.getTaskNodePath(taskId, TaskConfigurationNode.pause_date_period));
        pauseTimePeriod = nameSpaceDataManager.getData(TaskNodePath.getTaskNodePath(taskId, TaskConfigurationNode.pause_time_period));
        //
        String taskItemTotalCountStr = nameSpaceDataManager.getData(TaskNodePath.getTaskNodePath(taskId, TaskConfigurationNode.task_item_total_count));
        taskItemTotalCount = Integer.parseInt(taskItemTotalCountStr);
        taskItemIndexAndTaskItemParametersString = nameSpaceDataManager.getData(TaskNodePath.getTaskNodePath(taskId, TaskConfigurationNode.task_item_parameters));
        propertiesString = nameSpaceDataManager.getData(TaskNodePath.getTaskNodePath(taskId, TaskConfigurationNode.task_parameter));
        //
        String loadLevelStr = nameSpaceDataManager.getData(TaskNodePath.getTaskNodePath(taskId, TaskConfigurationNode.load_Level));
        loadLevel = Integer.valueOf(loadLevelStr);


        preferExecutorIdList = nameSpaceDataManager.getData(TaskNodePath.getTaskNodePath(taskId, TaskConfigurationNode.prefer_list));
        useDisExecutorIdList = Boolean.parseBoolean(nameSpaceDataManager.getData(TaskNodePath.getTaskNodePath(taskId, TaskConfigurationNode.use_dis_prefer_list)));
        //

        String timeoutMinutesStr = nameSpaceDataManager.getData(TaskNodePath.getTaskNodePath(taskId, TaskConfigurationNode.timeout_seconds));
        if (!Strings.isNullOrEmpty(timeoutMinutesStr)) {
            timeoutMinutes = Integer.parseInt(timeoutMinutesStr);
        }
    }


}